package com.jd.dl.web;

import java.io.ByteArrayInputStream;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.CookiesAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.jd.dl.pojo.Admin;
import com.jd.dl.service.AdminService;
import com.jd.dl.service.LoginNumberService;
import com.jd.dl.service.exception.ErrorCode;
import com.jd.dl.service.exception.ServiceException;
import com.jd.dl.web.interceptor.LoginInterceptor;
import com.opensymphony.xwork2.ActionSupport;

public class BackstageLoginAction extends ActionSupport implements ServletResponseAware,
		ServletRequestAware, SessionAware, CookiesAware {

	private String adminName;
	private String adminPWD;
	private String valCode;

	private AdminService adminService;
	private LoginNumberService loginNumberService;
	private ByteArrayInputStream imageStream;
	
	private static final String VALIDATE_CODE_KEY = "validatecodekey";
	
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
	public String excute(){
		return SUCCESS;
	}

	private boolean rememberMe;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private Map session;
	private Map cookies;
	private String goingToURL;

	public String getGoingToURL() {
		return goingToURL;
	}

	public void setGoingToURL(String goingToURL) {
		this.goingToURL = goingToURL;
	}

	public boolean isRememberMe() {
		return rememberMe;
	}

	public void setRememberMe(boolean rememberMe) {
		this.rememberMe = rememberMe;
	}

	public String login() {

		try {
			Object obj=session.get(LoginInterceptor.USER_SESSION_KEY);
			if(obj!=null&&!(obj instanceof Admin)){
				throw new ServiceException(ErrorCode.USER_NOT_EXIST,"请使用其他浏览器或关闭浏览器重新登陆！");
			}
			if(valCode == null || !valCode.equals(session.get(VALIDATE_CODE_KEY))){
				return INPUT;
			}
			
			Admin admin = adminService.login(adminName, adminPWD);
			if (rememberMe) {
				Cookie cookie = new Cookie(
						LoginInterceptor.COOKIE_REMEMBERME_KEY, admin.getAdminName()
								+ "==" + admin.getAdminPwd());
				cookie.setMaxAge(60 * 60 * 24 * 14);
				response.addCookie(cookie);
			}
			session.put(LoginInterceptor.USER_SESSION_KEY, admin);
			String goingToURL = (String) session
					.get(LoginInterceptor.GOING_TO_URL_KEY);
			//setDefaultFunction(user);
			if (goingToURL!=null&&!"".equals(goingToURL)) {
				//setGoingToURL(goingToURL);
				setGoingToURL("showMain");
				session.remove(LoginInterceptor.GOING_TO_URL_KEY);
			} else {
				setGoingToURL("showMain");
			}
			return SUCCESS;
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}

	}
	
	public String random(){
		try{
			loginNumberService.create();
			imageStream = loginNumberService.getImage();
			session.put(VALIDATE_CODE_KEY, loginNumberService.getString());
		}catch(Exception e){
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public void setCookiesMap(Map cookies) {
		this.cookies = cookies;
	}

	public String getAdminName() {
		return adminName;
	}

	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}

	public String getAdminPWD() {
		return adminPWD;
	}

	public void setAdminPWD(String adminPWD) {
		this.adminPWD = adminPWD;
	}

	public LoginNumberService getLoginNumberService() {
		return loginNumberService;
	}

	public void setLoginNumberService(LoginNumberService loginNumberService) {
		this.loginNumberService = loginNumberService;
	}

	public ByteArrayInputStream getImageStream() {
		return imageStream;
	}

	public void setImageStream(ByteArrayInputStream imageStream) {
		this.imageStream = imageStream;
	}

	public String getValCode() {
		return valCode;
	}

	public void setValCode(String valCode) {
		this.valCode = valCode;
	}

}
